<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img0.baidu.com/it/u=945789446,3441939734&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >Go数组&amp;切片</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、数组类型-Array"><a href="#一、数组类型-Array" class="headerlink" title="一、数组类型(Array)"></a>一、数组类型(Array)</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 数组是同一类型的元素集合(大小不可变化)</span><br><span class="line">	<span class="keyword">var</span> a [<span class="number">6</span>]<span class="keyword">int</span> </span><br><span class="line">	<span class="comment">// Go中数组下标从0开始，因此长度为n的数组下标范围：[0,n-1]</span></span><br><span class="line">	<span class="comment">// 整数浮点数数组中的元素默认初始化为0，字符串数组中的元素默认初始化为&quot;&quot; bool的默认初始化是false</span></span><br><span class="line">	<span class="comment">// 必须指定存放的元素的类型和容量(长度)</span></span><br><span class="line">	<span class="comment">// 数组的长度是数组类型的一部分</span></span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a [<span class="number">6</span>]<span class="keyword">int</span> <span class="comment">// 设置了int以后，如果想传入为int32的，就必须强转换</span></span><br><span class="line">        a[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">        <span class="comment">// 不支持索引为-1</span></span><br><span class="line">        <span class="comment">// a[-1] = 100</span></span><br><span class="line">        fmt.Print(a)</span><br><span class="line">    &#125;	</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 数组初始化</span><br><span class="line">	<span class="number">3.1</span>	</span><br><span class="line">	   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> a [<span class="number">2</span>]<span class="keyword">int</span> </span><br><span class="line">            a[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            a[<span class="number">1</span>] = <span class="number">100</span></span><br><span class="line">            a[<span class="number">2</span>] = <span class="number">100</span></span><br><span class="line">            fmt.Print(a)</span><br><span class="line">        &#125;	</span><br><span class="line">	<span class="number">3.2</span>	</span><br><span class="line">	   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> a [<span class="number">2</span>]<span class="keyword">int</span> </span><br><span class="line">            a[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            a[<span class="number">1</span>] = <span class="number">100</span></span><br><span class="line">            a[<span class="number">2</span>] = <span class="number">100</span></span><br><span class="line">            fmt.Print(a)</span><br><span class="line">        &#125;	</span><br><span class="line">	<span class="number">3.3</span></span><br><span class="line">	   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line">            fmt.Print(l)</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="number">3.4</span>	</span><br><span class="line">	   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125; <span class="comment">// ...的意思是在列表太长的情况下，Go去帮你算列表长度</span></span><br><span class="line">            fmt.Print(l)</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="number">3.5</span>	</span><br><span class="line">	   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">1</span>&#125; <span class="comment">// 除了0索引，其他的初始化</span></span><br><span class="line">            fmt.Print(l)</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="number">3.6</span>	</span><br><span class="line">	   <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">2</span>:<span class="number">1</span>&#125; <span class="comment">// 除了2索引，其他的初始化，也可以穿多个指定索引</span></span><br><span class="line">            fmt.Print(l)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 数组长度是类型的一部分</span><br><span class="line">            <span class="keyword">var</span> a [<span class="number">2</span>]<span class="keyword">int</span> </span><br><span class="line">            a[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            a[<span class="number">1</span>] = <span class="number">100</span></span><br><span class="line">            a[<span class="number">2</span>] = <span class="number">100</span></span><br><span class="line">		</span><br><span class="line">            <span class="keyword">var</span> b [<span class="number">5</span>]<span class="keyword">int</span> </span><br><span class="line">            b = a  <span class="comment">// 这是不行的，因为b与a的长度不相同！</span></span><br><span class="line"></span><br><span class="line"><span class="number">5.</span> <span class="built_in">len</span> 内置函数</span><br><span class="line">	<span class="comment">// len 是专门判断长度的</span></span><br><span class="line">	<span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">124</span>, <span class="number">5</span>, <span class="number">7</span>&#125;</span><br><span class="line">        fmt.Print(<span class="built_in">len</span>(l))</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.</span> 数组遍历(两种方法)</span><br><span class="line">	<span class="number">6.1</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">124</span>, <span class="number">5</span>, <span class="number">7</span>&#125;</span><br><span class="line">            <span class="keyword">for</span> i := <span class="number">0</span>; i&lt;<span class="built_in">len</span>(l); i++ &#123;</span><br><span class="line">                fmt.Printf(<span class="string">&quot;%v-&quot;</span>, l[i])</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="number">6.2</span></span><br><span class="line">		<span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">124</span>, <span class="number">5</span>, <span class="number">7</span>&#125;</span><br><span class="line">            <span class="keyword">for</span> index, value := <span class="keyword">range</span> l &#123;</span><br><span class="line">                fmt.Printf(<span class="string">&quot;index: %d, value: %d&quot;</span>, index, value)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="number">7.</span> 二维数组(注意:多维数组只有第一层可以使用...来让编译器推导数组长度)</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> l [<span class="number">3</span>][<span class="number">2</span>]<span class="keyword">int</span> = [<span class="number">3</span>][<span class="number">2</span>]<span class="keyword">int</span>&#123;</span><br><span class="line">                &#123;<span class="number">1</span>, <span class="number">2</span>&#125;,</span><br><span class="line">                &#123;<span class="number">3</span>, <span class="number">4</span>&#125;,</span><br><span class="line">                &#123;<span class="number">5</span>, <span class="number">6</span>&#125;,</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">for</span> index, value := <span class="keyword">range</span> l &#123;</span><br><span class="line">                <span class="keyword">for</span> a, b := <span class="keyword">range</span> value &#123;</span><br><span class="line">                    fmt.Printf(<span class="string">&quot;l[%d][%d]:[%d]\n&quot;</span>, index, a, b)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">8.</span> 数组是值类型</span><br><span class="line">    <span class="keyword">var</span> a [<span class="number">3</span>]<span class="keyword">int</span></span><br><span class="line">    a[<span class="number">0</span>] = <span class="number">10</span></span><br><span class="line">    a[<span class="number">1</span>] = <span class="number">20</span></span><br><span class="line">    a[<span class="number">2</span>] = <span class="number">30</span></span><br><span class="line">    b := a</span><br><span class="line">    <span class="comment">//b拷贝了数组a中所有元素</span></span><br><span class="line">    b[<span class="number">0</span>] = <span class="number">1000</span></span><br><span class="line">    fmt.Println(a, b) <span class="comment">// [12, 20, 40] [1000, 20, 40]</span></span><br><span class="line"></span><br><span class="line"><span class="number">9.</span> 参数传参也会拷贝</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a [<span class="number">3</span>]<span class="keyword">int</span></span><br><span class="line">        a[<span class="number">0</span>] = <span class="number">10</span></span><br><span class="line">        a[<span class="number">1</span>] = <span class="number">20</span></span><br><span class="line">        a[<span class="number">2</span>] = <span class="number">30</span></span><br><span class="line">        modify(a)</span><br><span class="line">        fmt.Println(a)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">modify</span><span class="params">(b [3]<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">        b[<span class="number">0</span>] = <span class="number">1000</span></span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">10</span>、注意</span><br><span class="line">    数组支持<span class="string">&quot;==&quot;</span>、 <span class="string">&quot;!=&quot;</span> 操作符，因为内存总是被初始化过的</span><br><span class="line">    [n]*T表示指针数组，*[n]T表示数组指针</span><br></pre></td></tr></table></figure>

<h2 id="二、切片"><a href="#二、切片" class="headerlink" title="二、切片"></a>二、切片</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 切片是基于数组类型做的一层封装，他简单灵活，可以自动扩容。</span><br><span class="line">	<span class="keyword">var</span> a []<span class="keyword">int</span></span><br><span class="line">	<span class="comment">// 切片是一个引用类型，它的内部结构包含地址、长度和容量，切片一般用于快速地操作一块数据集合</span></span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">	<span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a []<span class="keyword">int</span></span><br><span class="line">        <span class="keyword">var</span> b [<span class="number">3</span>]<span class="keyword">int</span> = [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line">        <span class="comment">// 从0下标开始一直到1下标，不包括2</span></span><br><span class="line">        a = b[<span class="number">0</span>:<span class="number">2</span>]</span><br><span class="line">        fmt.Print(a)</span><br><span class="line">        <span class="comment">// 从1下标开始一直到最后一个下标</span></span><br><span class="line">        a = b[<span class="number">1</span>:]</span><br><span class="line">        fmt.Print(a)</span><br><span class="line">        <span class="comment">// 从0下标一直到3下标</span></span><br><span class="line">        a = b[:<span class="number">3</span>]</span><br><span class="line">        fmt.Print(a)</span><br><span class="line">        <span class="comment">// 从一开始到最后下标</span></span><br><span class="line">        a = b[:]</span><br><span class="line">        fmt.Print(a)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 切片初始化</span><br><span class="line">	<span class="number">3.1</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>&#125;</span><br><span class="line">            <span class="keyword">var</span> s []<span class="keyword">int</span> = l[<span class="number">1</span>:<span class="number">2</span>]</span><br><span class="line">            fmt.Print(s)</span><br><span class="line">        &#125;</span><br><span class="line">	<span class="number">3.2</span> </span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            c := []<span class="keyword">int</span>&#123;<span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>&#125; <span class="comment">// 创建一个数组并返回一个切片， 不能对c进行后续操作了</span></span><br><span class="line">            fmt.Print(c)</span><br><span class="line">        &#125;</span><br><span class="line"><span class="number">4.</span> 切片修改</span><br><span class="line">	切片取来的值，修改会直接影响到数组，因为切片是引用类型，而不是单独创造</span><br><span class="line">	<span class="number">4.1</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := []<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;</span><br><span class="line">            <span class="keyword">var</span> s []<span class="keyword">int</span> = l[<span class="number">0</span>:<span class="number">2</span>]</span><br><span class="line">            s[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            fmt.Print(l)</span><br><span class="line">        &#125; <span class="comment">// l [100, 2, 3, 4]</span></span><br><span class="line">	<span class="number">4.2</span></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            numa := [<span class="number">3</span>]<span class="keyword">int</span>&#123;<span class="number">78</span>, <span class="number">79</span>, <span class="number">80</span>&#125;</span><br><span class="line">            <span class="comment">//创建2个切片，包含整个数组的所有元素</span></span><br><span class="line">            nums1 := numa[:]</span><br><span class="line">            nums2 := numa[:]</span><br><span class="line">            fmt.Println(<span class="string">&quot;array before change 1&quot;</span>, numa)</span><br><span class="line">            nums1[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            fmt.Println(<span class="string">&quot;array after modification to slice nums1&quot;</span>, numa)</span><br><span class="line">            nums2[<span class="number">1</span>] = <span class="number">101</span></span><br><span class="line">            fmt.Println(<span class="string">&quot;array after modification to slice nums2&quot;</span>, numa)</span><br><span class="line">            fmt.Println(numa)</span><br><span class="line">        &#125;  <span class="comment">// nums1 [100 79 80] nums2 [100 101 80] numa [100 101 80]</span></span><br><span class="line"></span><br><span class="line"><span class="number">5.</span> 使用<span class="built_in">make</span>创建切片</span><br><span class="line">	这个就很牛逼了，因为扩容就很方便，然后我们来探索把，接下来代码演示</span><br><span class="line">	<span class="number">5.1</span> 长度为多少只能添加多少个值</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="comment">// 用make内置函数创建一个切片 </span></span><br><span class="line">            i := <span class="built_in">make</span>([]<span class="keyword">int</span>, <span class="number">2</span>, <span class="number">3</span>) <span class="comment">// 括号内第二个参数为长度， 第三个参数为容量</span></span><br><span class="line">            <span class="comment">// 如果只有一个参数，那就是长度和容量都是这个参数</span></span><br><span class="line">            i[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 0]  len:2  capacity:3  addr:0xc0000ae078</span></span><br><span class="line">            i[<span class="number">1</span>] = <span class="number">200</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 200]  len:2  capacity:3  addr:0xc0000ae078</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.</span> 切片的再切片</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="keyword">var</span> s []<span class="keyword">int</span></span><br><span class="line">            <span class="keyword">var</span> l [<span class="number">6</span>]<span class="keyword">int</span> = [<span class="number">6</span>]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;</span><br><span class="line">            s = l[<span class="number">0</span>: <span class="number">4</span>]</span><br><span class="line">            fmt.Printf(<span class="string">&quot;len:%d, capacity:%d, %v\n&quot;</span>, <span class="built_in">len</span>(s), <span class="built_in">cap</span>(s), s)</span><br><span class="line">            <span class="comment">// len:4, capacity:6, [1 2 3 4]</span></span><br><span class="line">            a := s[<span class="number">0</span>: <span class="number">1</span>]</span><br><span class="line">            fmt.Printf(<span class="string">&quot;len:%d, capacity:%d, %v\n&quot;</span>, <span class="built_in">len</span>(a), <span class="built_in">cap</span>(a), a)</span><br><span class="line">            <span class="comment">// len:1, capacity:6, [1]</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.</span> 赋予新变量内存地址的变化</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a []<span class="keyword">int</span></span><br><span class="line">        <span class="keyword">var</span> l [<span class="number">6</span>]<span class="keyword">int</span> = [<span class="number">6</span>]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;</span><br><span class="line">        a = l[:]</span><br><span class="line">        b := a</span><br><span class="line">        fmt.Printf(<span class="string">&quot;%v, addr:%p\n&quot;</span>, a, a)</span><br><span class="line">        fmt.Printf(<span class="string">&quot;%v, addr:%p\n&quot;</span>, b, b)</span><br><span class="line">        a = <span class="built_in">append</span>(a, <span class="number">100</span>)</span><br><span class="line">        fmt.Printf(<span class="string">&quot;%v, addr:%p\n&quot;</span>, a, a)</span><br><span class="line">        fmt.Printf(<span class="string">&quot;%v, addr:%p\n&quot;</span>, b, b)</span><br><span class="line">        <span class="comment">/*[1 2 3 4 5 6], addr:0xc0000cc030</span></span><br><span class="line"><span class="comment">        [1 2 3 4 5 6], addr:0xc0000cc030</span></span><br><span class="line"><span class="comment">        [1 2 3 4 5 6 100], addr:0xc000086060</span></span><br><span class="line"><span class="comment">        [1 2 3 4 5 6], addr:0xc0000cc030</span></span><br><span class="line"><span class="comment">        也是能看出来一开始a跟b的内存地址是一样的</span></span><br><span class="line"><span class="comment">        但是在a进行超过容量的扩容后，a的内容地址就改了</span></span><br><span class="line"><span class="comment">        但是因为b没有改动，所以b的内存地址不变*/</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">8.</span> 空切片</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="comment">//定义names是一个空切片，长度和容量都等于0</span></span><br><span class="line">        <span class="comment">//不能说一个长度he容量都是0的切片一定是nil</span></span><br><span class="line">        <span class="comment">//不能对空切片进行访问，否则panic</span></span><br><span class="line">        <span class="keyword">var</span> names []<span class="keyword">string</span></span><br><span class="line">        <span class="keyword">if</span> names == <span class="literal">nil</span> &#123;</span><br><span class="line">            fmt.Println(<span class="string">&quot;slice is nil going to append&quot;</span>)</span><br><span class="line">            names = <span class="built_in">append</span>(names, <span class="string">&quot;John&quot;</span>, <span class="string">&quot;Sebastian&quot;</span>, <span class="string">&quot;Vinay&quot;</span>)</span><br><span class="line">            fmt.Println(<span class="string">&quot;names contents:&quot;</span>, names)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="comment">// nil可以判断是否为空</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="number">9.</span> 切片传参</span><br><span class="line">    <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">    <span class="keyword">import</span> (</span><br><span class="line">        <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="comment">//在函数内部修改numbers切片的值</span></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">subtactOne</span><span class="params">(numbers []<span class="keyword">int</span>)</span></span> &#123;</span><br><span class="line">        <span class="keyword">for</span> i := <span class="keyword">range</span> numbers &#123;</span><br><span class="line">            numbers[i] -= <span class="number">2</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        nos := []<span class="keyword">int</span>&#123;<span class="number">8</span>, <span class="number">7</span>, <span class="number">6</span>&#125;</span><br><span class="line">        fmt.Println(<span class="string">&quot;slice before function call&quot;</span>, nos)</span><br><span class="line">        subtactOne(nos)</span><br><span class="line">        <span class="comment">//nos修改生效了，说明切片是引用类型</span></span><br><span class="line">        fmt.Println(<span class="string">&quot;slice after function call&quot;</span>, nos)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">10.</span> 切片拷贝</span><br><span class="line">	<span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        veggies := []<span class="keyword">string</span>&#123;<span class="string">&quot;1&quot;</span>, <span class="string">&quot;2&quot;</span>, <span class="string">&quot;3&quot;</span>&#125;</span><br><span class="line">        fruits := []<span class="keyword">string</span>&#123;<span class="string">&quot;4&quot;</span>, <span class="string">&quot;5&quot;</span>&#125;</span><br><span class="line">        <span class="built_in">copy</span>(veggies, fruits)</span><br><span class="line">        fmt.Println(veggies, fruits)</span><br><span class="line">    &#125; <span class="comment">// [4 5 3] [4 5] 拷贝没有办法做扩容，只是对应下标替换了而已</span></span><br><span class="line"></span><br><span class="line"><span class="number">11.</span> 切片遍历</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a [<span class="number">3</span>]<span class="keyword">int</span></span><br><span class="line">        a[<span class="number">0</span>] = <span class="number">10</span></span><br><span class="line">        a[<span class="number">1</span>] = <span class="number">20</span></span><br><span class="line">        a[<span class="number">2</span>] = <span class="number">30</span></span><br><span class="line">        b := a[:]</span><br><span class="line">        <span class="keyword">for</span> index, val := <span class="keyword">range</span> b &#123;</span><br><span class="line">            fmt.Printf(<span class="string">&quot;index: %d, val: %d&quot;</span>, index, val)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="number">12.</span> 从切片中删除元素</span><br><span class="line">	Go语言中并没有删除切片元素的专用方法，我们可以使用切片本身的特性来删除元素。</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            x1 := [...]<span class="keyword">int</span> &#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>&#125;</span><br><span class="line">            s1 := x1[:]</span><br><span class="line">            fmt.Println(s1, <span class="built_in">len</span>(s1), <span class="built_in">cap</span>(s1))</span><br><span class="line">            <span class="comment">// 1. 切片不保存具体的值 </span></span><br><span class="line">            <span class="comment">// 2. 切片对应一个底层数组</span></span><br><span class="line">            <span class="comment">// 3. 底层数组都是占用一块连续的内存</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%p\n&quot;</span>, &amp;s1[<span class="number">0</span>])</span><br><span class="line">            s1 = <span class="built_in">append</span>(s1[:<span class="number">1</span>], s1[<span class="number">2</span>:]...) </span><br><span class="line">            <span class="comment">//这里删除了元素，但是也同时修改了底层数组!</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%p\n&quot;</span>, &amp;s1[<span class="number">0</span>])</span><br><span class="line">            fmt.Println(s1, <span class="built_in">len</span>(s1), <span class="built_in">cap</span>(s1))</span><br><span class="line">            fmt.Println(x1)</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">            [1 3 5] 3 3</span></span><br><span class="line"><span class="comment">            0xc000012168</span></span><br><span class="line"><span class="comment">            0xc000012168</span></span><br><span class="line"><span class="comment">            [1 5] 2 3</span></span><br><span class="line"><span class="comment">            [1 5 5]  这边数组变成 1 5 5 是因为数组元素底层是一块连续的内存</span></span><br><span class="line"><span class="comment">            */</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">13.</span> 关于<span class="built_in">append</span>删除切片中的某个元素</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        a1 := [...]<span class="keyword">int</span> &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>&#125;</span><br><span class="line">        s1 := a1[:]</span><br><span class="line">        <span class="comment">// 删除索引为1得2</span></span><br><span class="line">        s1 = <span class="built_in">append</span>(s1[<span class="number">0</span>:<span class="number">1</span>], s1[<span class="number">2</span>:]...)</span><br><span class="line">        fmt.Println(s1)</span><br><span class="line">        fmt.Println(a1)</span><br><span class="line">        <span class="comment">// 其实切片删除元素后，在底层数组把后面的值全部往前移，然后最后一个值依旧还是最后一个值</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="三、append-amp-copy"><a href="#三、append-amp-copy" class="headerlink" title="三、append &amp; copy"></a>三、append &amp; copy</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>利用<span class="built_in">append</span>内置方法去添加值</span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="comment">// 用make内置函数创建一个切片 </span></span><br><span class="line">            i := <span class="built_in">make</span>([]<span class="keyword">int</span>, <span class="number">2</span>, <span class="number">3</span>) <span class="comment">// 括号内第二个参数为长度， 第三个参数为容量</span></span><br><span class="line">            <span class="comment">// 如果只有一个参数，那就是长度和容量都是这个参数</span></span><br><span class="line">            i[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 0]  len:2  capacity:3  addr:0xc0000ae078</span></span><br><span class="line">            i[<span class="number">1</span>] = <span class="number">200</span></span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 200]  len:2  capacity:3  addr:0xc0000ae078</span></span><br><span class="line">            i = <span class="built_in">append</span>(i, <span class="number">300</span>)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 200 300]  len:3  capacity:3  addr:0xc000012168 这个时候超过长度就可以正常添加了</span></span><br><span class="line">            i = <span class="built_in">append</span>(i, <span class="number">400</span>)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 200 300 400]  len:4  capacity:6  addr:0xc00000a360</span></span><br><span class="line">            <span class="comment">// 这个时候甚至可以超过容量，但是首先容量成倍增加了，然后内存地址也改变了，这就说明Go内部帮你重新创建了</span></span><br><span class="line">            i = <span class="built_in">append</span>(i, <span class="number">500</span>, <span class="number">600</span>)</span><br><span class="line">            fmt.Printf(<span class="string">&quot;%v  len:%d  capacity:%d  addr:%p  &quot;</span>, i, <span class="built_in">len</span>(i), <span class="built_in">cap</span>(i), i)</span><br><span class="line">            <span class="comment">// [100 200 300 400 500 600]  len:6  capacity:6  addr:0xc0000d0030 </span></span><br><span class="line">            <span class="comment">// appen支持传入多个值，内存地址也随之继续改变</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> <span class="built_in">append</span>一个切片</span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> a []<span class="keyword">int</span> = []<span class="keyword">int</span>&#123;<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;</span><br><span class="line">        <span class="keyword">var</span> b []<span class="keyword">int</span> = []<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line">        <span class="comment">// 这样会报错</span></span><br><span class="line">        a.<span class="built_in">append</span>(a, b)</span><br><span class="line">        <span class="comment">// 正确应该这么写</span></span><br><span class="line">        a.<span class="built_in">append</span>(a, b...) <span class="comment">//...把切片b展开一个个元素</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span><span class="built_in">copy</span></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="comment">// 定义一个数组</span></span><br><span class="line">        a1 := []<span class="keyword">int</span> &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;</span><br><span class="line">        <span class="comment">// 给a2赋值</span></span><br><span class="line">        a2 := a1</span><br><span class="line">        <span class="comment">// 定a3数组</span></span><br><span class="line">        <span class="keyword">var</span> a3 = <span class="built_in">make</span>([]<span class="keyword">int</span>, <span class="number">3</span>)</span><br><span class="line">        <span class="built_in">copy</span>(a3, a1) <span class="comment">// copy</span></span><br><span class="line">        fmt.Println(a1, a2, a3)</span><br><span class="line">        a1[<span class="number">0</span>] = <span class="number">100</span></span><br><span class="line">        fmt.Println(a1, a2, a3)</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">        [1 2 3] [1 2 3] [1 2 3]</span></span><br><span class="line"><span class="comment">        [100 2 3] [100 2 3] [1 2 3]</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="四、切片的本质"><a href="#四、切片的本质" class="headerlink" title="四、切片的本质"></a>四、切片的本质</h2><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">``</span><span class="string">`</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">## 五、课后小作业</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">`</span><span class="string">``</span><span class="keyword">go</span></span><br><span class="line"><span class="number">1.</span> 求数组元素之和</span><br><span class="line">        <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">        <span class="keyword">import</span> (</span><br><span class="line">            <span class="string">&quot;fmt&quot;</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;</span><br><span class="line">            <span class="keyword">var</span> num <span class="keyword">int</span> = <span class="number">0</span></span><br><span class="line">            <span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(l); i++ &#123;</span><br><span class="line">                num += l[i]</span><br><span class="line">            &#125;</span><br><span class="line">            fmt.Print(num)</span><br><span class="line">        &#125;</span><br><span class="line"><span class="number">2.</span> 找出数组中和为给定值的两个元素的下标，比如数组:[<span class="number">1</span>,<span class="number">3</span>,<span class="number">5</span>,<span class="number">8</span>,<span class="number">7</span>]，找出两个元素之和等于<span class="number">8</span>的下标分别是(<span class="number">0</span>, <span class="number">4</span>)和(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line">        <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">        <span class="keyword">import</span> (</span><br><span class="line">            <span class="string">&quot;fmt&quot;</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            l := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">8</span>, <span class="number">7</span>&#125;</span><br><span class="line">            <span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="built_in">len</span>(l); i++ &#123;</span><br><span class="line">                <span class="keyword">for</span> num := <span class="number">0</span>; num &lt; <span class="built_in">len</span>(l); num++ &#123;</span><br><span class="line">                    <span class="keyword">if</span> l[i]+l[num] == <span class="number">8</span> &#123;</span><br><span class="line">                        fmt.Printf(<span class="string">&quot;(%d, %d)\n&quot;</span>, i, num)</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="number">3.</span> 下列程序输出什么</span><br><span class="line">	<span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">    <span class="keyword">import</span> (</span><br><span class="line">        <span class="string">&quot;fmt&quot;</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">        <span class="keyword">var</span> sa = <span class="built_in">make</span>([]<span class="keyword">string</span>, <span class="number">5</span>, <span class="number">10</span>)</span><br><span class="line">        fmt.Println(sa)</span><br><span class="line">        <span class="keyword">for</span> i := <span class="number">0</span>; i &lt; <span class="number">10</span>; i++ &#123;</span><br><span class="line">            sa = <span class="built_in">append</span>(sa, fmt.Sprintf(<span class="string">&quot;%v&quot;</span>, i))</span><br><span class="line">        &#125;</span><br><span class="line">        fmt.Println(sa)</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">// [    ]</span></span><br><span class="line">	<span class="comment">// [     0 1 2 3 4 5 6 7 8 9]</span></span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 使用golang标准包 “sort”对数组进行排序(需自主学习，锻炼能力)</span><br><span class="line">        <span class="keyword">package</span> main</span><br><span class="line"></span><br><span class="line">        <span class="keyword">import</span> (</span><br><span class="line">            <span class="string">&quot;fmt&quot;</span></span><br><span class="line">            <span class="string">&quot;sort&quot;</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">func</span> <span class="title">main</span><span class="params">()</span></span> &#123;</span><br><span class="line">            <span class="comment">// 对整形数组排序</span></span><br><span class="line">            s1 := [...]<span class="keyword">int</span>&#123;<span class="number">1</span>, <span class="number">3</span>, <span class="number">43</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">32</span>, <span class="number">56</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">53</span>&#125;</span><br><span class="line">            sort.Ints(s1[:]) <span class="comment">// 切片本质上是对底层数组的一个view，不能直接修改view，通过视图来修改</span></span><br><span class="line">            fmt.Println(s1)  <span class="comment">// [1 3 3 5 6 7 8 32 43 53 56]</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 对字符串类型数组进行排序</span></span><br><span class="line">            s2 := [...]<span class="keyword">string</span>&#123;<span class="string">&quot;mashiro&quot;</span>, <span class="string">&quot;satori&quot;</span>, <span class="string">&quot;hatsunemiku&quot;</span>&#125;</span><br><span class="line">            sort.Strings(s2[:])</span><br><span class="line">            fmt.Println(s2) <span class="comment">// [hatsunemiku mashiro satori]</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 对浮点数类型数组进行排序</span></span><br><span class="line">            s3 := [...]<span class="keyword">float64</span>&#123;<span class="number">12.3</span>, <span class="number">31.31</span>, <span class="number">312.2</span>, <span class="number">1.1</span>, <span class="number">43</span>, <span class="number">2</span>&#125;</span><br><span class="line">            sort.Float64s(s3[:])</span><br><span class="line">            fmt.Println(s3) <span class="comment">// [1.1 2 12.3 31.31 43 312.2]</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 查找一个int在数组中的索引</span></span><br><span class="line">            s4 := [...]<span class="keyword">int</span>&#123;<span class="number">2</span>, <span class="number">3</span>, <span class="number">452</span>, <span class="number">1</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">67</span>&#125;</span><br><span class="line">            index := sort.SearchInts(s4[:], <span class="number">3</span>)</span><br><span class="line">            fmt.Println(index) <span class="comment">// 如果有多个，只返回第一个</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">// 查找一个string在数组中的位置</span></span><br><span class="line">            s5 := [...]<span class="keyword">string</span>&#123;<span class="string">&quot;mashiro&quot;</span>, <span class="string">&quot;satori&quot;</span>, <span class="string">&quot;hatsunemiku&quot;</span>&#125;</span><br><span class="line">            index1 := sort.SearchStrings(s5[:], <span class="string">&quot;mashiro&quot;</span>)</span><br><span class="line">            fmt.Println(index1) <span class="comment">// 0</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2020/03/04/Go%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B------%E6%95%B0%E7%BB%84&amp;%E5%88%87%E7%89%87/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2020/03/04/Go%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B------%E6%95%B0%E7%BB%84&amp;%E5%88%87%E7%89%87/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
